package com.example.queryexpressdelivery_01.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.entity.UserData;

import java.util.List;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {

    private List<UserData> userList;
    private OnItemClickListener listener;

    public UserAdapter(List<UserData> userList) {
        this.userList = userList;
    }

    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
        return new UserViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
        UserData user = userList.get(position);
        holder.tvUserId.setText(String.valueOf(user.getId()) + "\t\t\t\t");
        holder.tvAccount.setText(user.getAccount() + "\t\t\t\t");
        holder.tvPhone.setText(user.getPhone() + "\t\t\t\t");
        holder.tvEmail.setText(user.getEmail() + "\t\t\t\t");
        holder.tvStatus.setText(user.getStatus() + "\t\t\t\t");
        // If you want to display 'data' field, uncomment the following line:
         holder.tvData.setText(user.getData() + "\t\t\t\t");
    }

    @Override
    public int getItemCount() {
        return userList != null ? userList.size() : 0;
    }

    public class UserViewHolder extends RecyclerView.ViewHolder {
        TextView tvUserId;
        TextView tvAccount;
        TextView tvPhone;
        TextView tvEmail;
        TextView tvStatus;
        // Uncomment the following line if you want to display 'data' field
         TextView tvData;

        public UserViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUserId = itemView.findViewById(R.id.tvUserId);
            tvAccount = itemView.findViewById(R.id.tvAccount);
            tvPhone = itemView.findViewById(R.id.tvPhone);
            tvEmail = itemView.findViewById(R.id.tvEmail);
            tvStatus = itemView.findViewById(R.id.tvStatus);
            // Uncomment the following line if you want to display 'data' field
             tvData = itemView.findViewById(R.id.tvData);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (listener != null && position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(userList.get(position));
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(UserData user);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
